﻿
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Zzj.Model;
using Zzj.Utils;

namespace Zzj.Windows
{
    /// <summary>
    /// PrintWindow.xaml 的交互逻辑
    /// </summary>
    public partial class PrintWindow : Window
    {
        SynchronizationContext _syncContext = null;
        BillModel billModel;
        string printTpls;
        string type;
        public PrintWindow()
        {
            InitializeComponent();
            _syncContext = SynchronizationContext.Current;

            this.Loaded += Win_Loaded;
        }

        private void Win_Loaded(object sender, RoutedEventArgs e)
        {
            new Thread(() =>
            {
                string[] tplArray = printTpls.Split(',');
                foreach (string tpl in tplArray)
                {
                    doPrint(tpl);
                }
                //Thread.Sleep(1000);
                
                _syncContext.Post(CloseWindow, "success");

                JObject paramJSON = new JObject();
                paramJSON.Add("id", billModel.Id);
                HttpPostHelper.doHttpPost("/qd-s-factory/makeCard/queryBillSelfPrint", paramJSON.ToString());

            }).Start();
        }
        private void doPrint(string tpl)
        {
            PrintUtils.printBill(tpl, billModel,type);
        }
        public void setTemplete(string tpls,string printType)
        {
            printTpls = tpls;
            type = printType;
        }
        public void setBillMode(BillModel bill)
        {
            billModel = bill;
        }
        private void CloseWindow(object text)
        {
            string status = (string)text;
            if ("success".Equals(status))
            {
                this.DialogResult = true;
            }
            this.Close();
        }
    }
}
